package cn.jacksigxu.min3halla.block.drink;

import cn.jacksigxu.min3halla.init.MHBlockStateProperties;
import cn.jacksigxu.min3halla.item.BigDrinkItem;
import cn.jacksigxu.min3halla.item.DrinkItem;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

import javax.annotation.Nullable;

@SuppressWarnings("deprecation")
public class SizedDrinkBlock extends BasicDrinkBlock {

    public static final BooleanProperty DOUBLE_INGREDIENTS = MHBlockStateProperties.DOUBLE_INGREDIENTS;

    protected final VoxelShape bigShapeNS;
    protected final VoxelShape bigShapeEW;

    public SizedDrinkBlock(VoxelShape shape, VoxelShape bigShape) {
        super(shape);
        this.bigShapeNS = bigShape;
        this.bigShapeEW = bigShape;
        this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(DOUBLE_INGREDIENTS, false);
    }

    public SizedDrinkBlock(VoxelShape shapeNS, VoxelShape shapeEW, VoxelShape bigShapeNS, VoxelShape bigShapeEW) {
        super(shapeNS, shapeEW);
        this.bigShapeNS = bigShapeNS;
        this.bigShapeEW = bigShapeEW;
        this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(DOUBLE_INGREDIENTS, false);
    }

    @Override
    public ItemStack getCloneItemStack(BlockGetter pLevel, BlockPos pPos, BlockState pState) {
        var stack = super.getCloneItemStack(pLevel, pPos, pState);
        if (pState.getValue(DOUBLE_INGREDIENTS)) {
            stack.getOrCreateTag().putBoolean("Big", true);
        }
        return stack;
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
        pBuilder.add(FACING).add(DOUBLE_INGREDIENTS);
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext pContext) {
        var state = this.defaultBlockState().setValue(FACING, pContext.getHorizontalDirection().getOpposite());
        var item = pContext.getItemInHand();
        if (item.getItem() instanceof DrinkItem && !(item.getItem() instanceof BigDrinkItem)) {
            state = state.setValue(DOUBLE_INGREDIENTS, item.getTag() != null && item.getTag().contains("Big") && item.getTag().getBoolean("Big"));
        }
        return state;
    }

    @Override
    public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
        if (pState.getValue(DOUBLE_INGREDIENTS)) {
            var direction = pState.getValue(FACING);
            if (direction.getAxis() == Direction.Axis.X) {
                return this.bigShapeNS;
            }
            return this.bigShapeEW;
        }
        return super.getShape(pState, pLevel, pPos, pContext);
    }
}
